package com.seagull.sb.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seagull.sb.bo.StudentBO;
import com.seagull.sb.domain.Student;
import com.seagull.sb.dto.StudentDto;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.ServiceMapper;

@Component
@Path("/student")
public class StudentService extends BaseService {

	@Autowired
	StudentBO studentBO;
	
	private ServiceMapper sm = ServiceMapper.getInstaMapper();
	

	@GET
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	public List<StudentDto> getStudent() {
		SBRequestContext rc = new SBRequestContext();
		rc.setOrgId(1);
		List<Student> students = studentBO.getStudents(rc);
		return sm.mapToStudentDto(students);

	}
	
	@GET
	@Path("/search/{searchstring}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<StudentDto> getStudentByKeyword(@PathParam("searchstring") String searchString) {
		SBRequestContext rc = new SBRequestContext();
		rc.setOrgId(1);
		rc.setClassId(1);
		List<Student> students = studentBO.getStudents(rc, searchString);
		return sm.mapToStudentDto(students);

	}
	
	@GET
	@Path("{classid}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<StudentDto> getStudentByClass(@PathParam("classid") int classid) {
		SBRequestContext rc = new SBRequestContext();
		rc.setOrgId(1);
		rc.setClassId(classid);
		List<Student> students = studentBO.getStudents(rc);
		return sm.mapToStudentDto(students);

	}
 

	
	@POST
	@Path("/")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public StudentDto saveStudent(StudentDto studentDto) {
		SBRequestContext rc = new SBRequestContext();
		Student student = sm.mapToStudent(studentDto);
		studentBO.saveStudent(rc, student);
		System.out.println("saveStudent - Successful.");
		return studentDto;
 	}
	
	
 
}